import {Component, OnInit, Input, EventEmitter} from '@angular/core';
import {Movie} from '../movie'
import {ActivatedRoute} from '@angular/router';
import {Location} from '@angular/common';
import {MoviesService} from '../movies.service'
import {NzMessageService} from 'ng-zorro-antd/message';
import {Router} from '@angular/router'; //导入router服务
@Component({
  selector: 'app-movie-detail',
  templateUrl: './movie-detail.component.html',
  styleUrls: ['./movie-detail.component.css']
})
export class MovieDetailComponent implements OnInit {
  // @Input() movie:Movie;
  mid: number;
  username: '';
  movies: Movie[];
  movie: Movie;
  userTags: [];
  allTags: [];
  myTag: '';
  page = 0;
  size = 6;
  showMovies = [];

  constructor(private route: ActivatedRoute,
              private location: Location,
              private moviesService: MoviesService,
              private message: NzMessageService,
              private router: Router) {
  }

  ngOnChanges(): void {
    this.mid = +this.route.snapshot.paramMap.get('id');
    this.username = window.localStorage['username'];
    this.getMovie();
    this.getUserTags();
    this.getAllTags();
    this.getSimilarMoviesById();
  }

  ngDoCheck() {
  }

  ngAfterContentInit() {

  }

  ngOnInit(): void {
    this.mid = +this.route.snapshot.paramMap.get('id');
    this.username = window.localStorage['username'];
    this.getMovie();
    this.getUserTags();
    this.getAllTags();
    this.getSimilarMoviesById();
  }

  //获取电影详情
  getMovie() {
    this.moviesService.getMovieDetailById(this.mid, this.username).subscribe(res => {
      console.log("具体电影：", res);
      if (res.code == 0) {
        this.movie = res.data;
      } else {
        this.message.create('error', res.msg);
      }
    })
  }

  //获取用户对该电影的标签
  getUserTags() {
    this.moviesService.getUserTags(this.mid, this.username).subscribe(res => {
      console.log("用户的标签：", res);
      if (res.code == 0) {
        this.userTags = res.data;
      } else {
        this.userTags = null;
        //暂无标签的情况
        //this.message.create('error',res.msg);
      }
    })
  }

  //获取其他人对该电影的标签
  getAllTags() {
    this.moviesService.getAllTags(this.mid, this.username).subscribe(res => {
      console.log("大家的标签：", res);
      if (res.code == 0) {
        this.allTags = res.data;
      } else {
        this.allTags = null;
        console.log("暂无标签:", this.allTags);
        //暂无标签的情况
      }
    })
  }

  //获取该电影的相似推荐
  getSimilarMoviesById() {
    this.moviesService.getSimilarMoviesById(this.mid).subscribe(res => {
      console.log("该电影的相似推荐:", res);
      if (res.code == 0) {
        this.movies = res.data;
        this.page = 0;
        this.getShowMovies();
      } else {

      }
    })
  }

  //为该电影添加标签
  addTag() {
    this.moviesService.addTagForMovie(this.mid, this.username, this.myTag).subscribe(res => {
      console.log("添加标签结果：", res);
      if (res.code == 0) {
        this.message.create('success', res.data);
        this.getUserTags();
      } else {
        this.message.create('error', res.msg);
      }
    })
  }

  //为电影评分
  scoreMovie(t) {
    console.log("评分：", t);
    this.moviesService.scoreMovie(this.username, this.mid, t).subscribe(res => {
      console.log("评分结果：", res);
      if (res.code == 0) {
        this.message.create('success', res.data);
      } else {
        this.message.create('error', res.msg);
      }
    })
  }

  //截取显示的推荐电影
  getShowMovies() {
    this.showMovies = this.movies.slice(this.page * this.size, (this.page + 1) * this.size);
  }

  //前翻
  pagePre() {
    if (this.page === 0) {
      this.message.create('warning', '翻到头了哦！');
    } else {
      this.page--;
      this.getShowMovies();
    }
  }

  //后翻
  pageDre() {
    let end = this.movies.length / this.size;
    if (this.page >= end - 1) {
      this.message.create('warning', '翻到底了哦！');
    } else {
      this.page++;
      this.getShowMovies();
    }
  }

  goBack() {
    this.location.back();
  }

  getToSimilar(m: Movie) {
    console.log("传参：", m);
    this.router.navigateByUrl('/home/detail/' + m.mid).then(x => window.location.reload());
    // window.location.reload()
  }

}
